<!-- 优惠券管理 -->

<template>
    <div id="template">
        <page-main>
            <avue-crud
                ref="crud"
                v-model="Main_Form"
                :option="Main_Option"
                :data="Main_Data"
                :page.sync="Main_Page"
                :search.sync="Main_search"
                :table-loading="main_table_loading"
                @refresh-change="refreshChange"
                @search-change="searchChange"
                @reset-change="resetChange"
                @row-save="rowSave"
                @row-update="rowUpdate"
                @row-del="rowDel"
                @size-change="sizeChange"
                @current-change="currentChange"
            >
            </avue-crud>
        </page-main>
    </div>
</template>

<script>
import { Main_Option, Main_Data } from "./config";
import {
    queryCouponsApi,
    createCouponsApi,
    updateCouponsApi,
    deleteCouponsApi,
} from "@/api/couponsManage/index.js";

export default {
    data() {
        return {
            Main_Option,
            Main_Data,
            main_table_loading: false,
            Main_Form: {},
            Main_search: {},
            Main_Page: {
                currentPage: 1,
                pageSize: 10,
                total: 1,
            },
        };
    },
    mounted() {
        this.getHomePageInfoFunc();
    },
    methods: {
        // 搜索事件
        async searchChange(params, done) {
            this.Main_Page.currentPage = 1;
            this.Main_Page.pageSize = 10;
            await this.getHomePageInfoFunc();
            done();
        },
        currentChange(val) {
            this.Main_Page.currentPage = val;
            this.getHomePageInfoFunc();
        },

        sizeChange(val) {
            this.Main_Page.pageSize = val;
            this.getHomePageInfoFunc();
        },
        // 重置
        resetChange() {
            this.Main_search = {};
        },
        // 刷新
        refreshChange() {
            this.Main_Page.currentPage = 1;
            this.getHomePageInfoFunc();
        },
        // 新增
        rowSave(form, done, loading) {
            const { title, price, type, end_time } = form;
            this.createInterfaceFunc({ title, price, type, end_time}, done, loading);
        },

        rowUpdate(row, index, done, loading) {
            const { id, title, price, type, end_time } = row;
            this.updateInterfaceFunc({ id, title, price, type, end_time }, done, loading);
        },

        rowDel(row) {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.deleteInterfaceFunc({ id: row?.id ?? "" });
                })
                .catch(() => {
                    this.$message.info("已取消");
                });
        },

        /**
         * @name requests
         * @author --h28
         */
        // 获取主页
        async getHomePageInfoFunc() {
            this.main_table_loading = true;
            const response = await queryCouponsApi({
                ...this.Main_search,
                currentPage: this?.Main_Page?.currentPage ?? 1,
                pageSize: this?.Main_Page?.pageSize ?? 10,
            }).catch(() => {});

            if (response?.code === 200) {
                this.Main_Data = response?.data?.records ?? [];
                this.Main_Page.total = response?.data?.total ?? 0;
            }
            this.main_table_loading = false;
        },
        // 新增
        async createInterfaceFunc(data, done, loading) {
            const response = await createCouponsApi(data).catch(() => {});
            if (response?.code === 200) {
                this.$notify.success({ title: "提示", message: "新增成功" });
                this.Main_Page.currentPage = 1;
                this.getHomePageInfoFunc();
                done();
            }
            loading();
        },
        // 编辑
        async updateInterfaceFunc(data, done, loading) {
            const response = await updateCouponsApi(data).catch(() => {});
            if (response?.code === 200) {
                this.$notify.success({ title: "提示", message: "编辑成功" });
                this.getHomePageInfoFunc();
                done();
            }
            loading();
        },
        // 删除
        async deleteInterfaceFunc(data) {
            const loadding = this.$loading({
                lock: true,
                text: "加载中...",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)",
            });
            const response = await deleteCouponsApi(data).catch(() => {});
            if (response?.code === 200) {
                this.$notify.success({ title: "提示", message: "删除成功" });
                this.Main_Page.currentPage = 1;
                this.getHomePageInfoFunc();
            }
            loadding.close();
        },
    },
};
</script>

<!-- <style>
  
    </style> -->
